package com.sd365.permission.centre.service.util;/*
 * @Auther:hantianbing
 * @Date:2022/12/15
 * @Description:用户角色工具类
 */

import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.permission.centre.pojo.dto.RoleResourceDTO;
import com.sd365.permission.centre.pojo.dto.UserDTO;
import com.sd365.permission.centre.pojo.dto.UserRoleDTO;
import com.sd365.permission.centre.service.exception.UserCentreExceptionCode;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserRoleUtils
 * @Description 用户角色工具类
 * @Author hantianbing
 * @Date 2022/12/15 18:11
 **/
@Component
public class UserRoleResourceUtils {


    /**
     * @Description 从UserDTO数组中获得userIds
     *
     * @Param
     * @return java.util.List<java.lang.Long>
     * @Date 2022/12/16 10:00
     * @Author hantianbing
     **/
    public static List<Long> getUserIdsFromUserDTOArray(UserDTO[] userDTO){
        //非空判断
        if(userDTO == null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_USER_MODULE_USER_DTO_NULL, new Exception("userDTO是空的"));
        }
        List<Long> userIds = new ArrayList<>();
        for (UserDTO dto : userDTO) {
            userIds.add(dto.getId());
        }
        return userIds;
    }


    /***
     * @Description 从userRoleDTO中获取所有的userIds
     *
     * @Param
     * @return java.util.List<java.lang.Long>
     * @Date 2022/12/16 9:52
     * @Author hantianbing
     **/
    public static List<Long> getUserIdsFromUserRoleDTO(UserRoleDTO userRoleDTO){
        List<Long> userIds = userRoleDTO.getUserIds();
        if(userIds == null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_USER_MODULE_USER_NULL, new Exception("用户id是空的"));
        }
        return userIds;
    }


    /**
     * 获得所有的roleIds
     * @param userRoleDTO
     * @return
     */
    public static List<Long> getRoleIdsFromUserRoleDTO(UserRoleDTO userRoleDTO){
        List<Long> roleIds = userRoleDTO.getRoleIds();
        if(roleIds ==null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_ROLE_MODULE_ROLE_NULL, new Exception("角色为空"));
        }
        return roleIds;
    }

    /***
     * @Description 查询所有的角色id
     *
     * @Param
     * @return java.util.List<java.lang.Long>
     * @Date 2022/12/15 18:25
     * @Author hantianbing
     **/
    public static List<Long> getRoleIdsFromRoleResource(RoleResourceDTO roleResourceDTO){
        List<Long> roleIds = roleResourceDTO.getRoleIds();
        if(roleIds == null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_ROLE_MODULE_ROLE_NULL, new Exception("角色为空"));
        }
        return roleIds;
    }

    /***
     * @Description 查询所有的资源信息
     *
     * @Param
     * @return java.util.List<java.lang.Long>
     * @Date 2022/12/15 18:23
     * @Author hantianbing
     **/
    public static List<Long> getResourceIdsFromRoleResource(RoleResourceDTO roleResourceDTO){
        List<Long> resourceIds = roleResourceDTO.getResourceIds();
        if(resourceIds == null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_ROLE_MODULE_RESOURCE_NULL, new Exception("资源为空"));
        }
        return resourceIds;
    }
}
